<?php

class ZON_property_widget extends WP_Widget {
	public function __construct() {
		parent::__construct( 'widget_properties',
							 'Zonthemes - Widget Properties',
							 array( 'description' => __( 'Widget display property in sidebar (Popular and Lastest Properties)' ), ) 
		);
	}
	public function widget( $args, $instance ) {
		extract( $args );
		$title = apply_filters( 'title', $instance['title'] );
		$showposts = apply_filters( 'showposts', $instance['showposts'] );
		$type = apply_filters( 'type', $instance['type'] );
		$layout = apply_filters( 'layout', $instance['layout'] );
		if($type == 'lastest'){
			$args = array(
				'showposts' => $showposts,
				'post_type' => 'property'
			);
		}else{
			$args = array(
				'showposts' => $showposts,
				'post_type' => 'property',
				'meta_key'  => 'post_views_count',
				'orderby'   => 'meta_value_num',
				'order'     => 'DESC'
			);
		}
		?>
		<div class="sidebar-box pro">
			<h2 class="sidebar-title"><?php echo $title; ?></h2>
			<ul class="<?php if($layout == 'right'){ echo 'right-images'; }else{if($layout == 'full'){ echo 'full-images'; }else{ echo 'left-images';}} ?>">
			<?php 
			$query = new WP_Query( $args);
			if ($query->have_posts()) : while ($query->have_posts()) : $query->the_post();?>
				<li>
					<div class="sidebar-li-hover">
						<a href="<?php the_permalink(); ?>" title="<?php the_title(); ?>">
							<?php if($layout == 'full'){ ?>
								<img src="<?php echo zon_get_thumbnail('medium'); ?>" alt="<?php the_title(); ?>" title="<?php the_title(); ?>" />
							<?php }else{ ?>
								<img src="<?php echo zon_get_thumbnail('thumbnail'); ?>" alt="<?php the_title(); ?>" title="<?php the_title(); ?>" />
							<?php } ?>
						</a>
						<p class="sidebar-p-title"><a href="<?php the_permalink(); ?>" title="<?php the_title(); ?>"><?php the_title(); ?></a></p>
						<p><?php echo zon_meta('zon_address'); ?></p>
						<p class="price">$<?php echo zon_meta('zon_price'); ?></p>
						<div class="clear"></div>
					</div>
				</li>
			<?php endwhile; endif; ?>
			</ul>
		</div>
		<?php
	}
	public function update( $new_instance, $old_instance ) {
		$instance = $old_instance;
		$instance['title'] = strip_tags( $new_instance['title'] );
		$instance['showposts'] = strip_tags( $new_instance['showposts'] );
		$instance['type'] = strip_tags( $new_instance['type'] );
		$instance['layout'] = strip_tags( $new_instance['layout'] );
		return $instance;
	}
	public function form( $instance ) {
		$defaults = array( 'title' =>__( 'Widget Properties' , 'tie'), 'showposts' =>__( '5' , 'tie' ) );
		$showposts = $instance['showposts']; 
		$title = $instance['title'];
		$type = $instance['type'];
		$layout = $instance['layout'];
		?>
		<p>
			<label for="<?php echo $this->get_field_id( 'title' ); ?>">Title : </label>
			<input id="<?php echo $this->get_field_id( 'title' ); ?>" name="<?php echo $this->get_field_name( 'title' ); ?>" value="<?php echo $instance['title']; ?>" class="widefat" type="text" />
		</p>

		<p>
			<label for="<?php echo $this->get_field_id( 'type' ); ?>">Choose widget : </label>
			<select id="<?php echo $this->get_field_id( 'type' ); ?>" name="<?php echo $this->get_field_name( 'type' ); ?>">
			    <option value="popular" <?php if($instance['type'] == 'popular'){ ?> selected="selected" <?php } ?> >Popular Properties</option>
			    <option value="lastest" <?php if($instance['type'] == 'lastest'){ ?> selected="selected" <?php } ?> >Lastest Properties</option>
			</select>
		</p>

		<p>
			<label for="<?php echo $this->get_field_id( 'layout' ); ?>">Choose layout : </label>
			<select id="<?php echo $this->get_field_id( 'layout' ); ?>" name="<?php echo $this->get_field_name( 'layout' ); ?>">
			    <option value="left" <?php if($instance['layout'] == 'left'){ ?> selected="selected" <?php } ?>>Images Left</option>
			    <option value="right" <?php if($instance['layout'] == 'right'){ ?> selected="selected" <?php } ?>>Images right</option>
			    <option value="full" <?php if($instance['layout'] == 'full'){ ?> selected="selected" <?php } ?>>Images full</option>
			</select>
		</p>

		<p>
			<label for="<?php echo $this->get_field_id( 'showposts' ); ?>">Showposts : </label>
			<input id="<?php echo $this->get_field_id( 'showposts' ); ?>" name="<?php echo $this->get_field_name( 'showposts' ); ?>" value="<?php echo $instance['showposts']; ?>" class="widefat" type="text" />
		</p>
		<?php
	}
}
add_action( 'widgets_init', create_function( '', 'register_widget( "ZON_property_widget" );' ) );
?>